-
Notifications
You must be signed in to change notification settings - Fork 8.2k
Enable quad and octal mode in spi_nor.c to support QPI Flash and Octal Flash #65659
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Enable quad and octal mode in spi_nor.c to support QPI Flash and Octal Flash #65659
Conversation
|
Hello @daniel-0723, and thank you very much for your first pull request to the Zephyr project! |
|
Thanks for this contribution. Please:
Also, your PR introduces a lot of changes that would deserve an individual explanation. So please split your change into multiple commits with proper justification for each of them. |
drivers/spi/ospi_ll_stm32.c
Outdated
| @@ -0,0 +1,388 @@ | |||
| /* | |||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There is already a OSPI STM32 driver (see drivers/flash/flash_stm32_ospi.c), please explain the added value of this one and why the existing one can't be fixed.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can refer to this link for more information.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There is already a OSPI STM32 driver (see drivers/flash/flash_stm32_ospi.c)
What we wanted to do before was to make spi_nor.c compatible with ospi controller, so we took STM32 ospi as an example. And implemented ospi_ll_stm32.c which is partially porting from (drivers/flash/flash_stm32_ospi.c), provided interface for spi_nor.c to call.
drivers/spi/ospi_ll_stm32.c
Outdated
| @@ -0,0 +1,388 @@ | |||
| /* | |||
| * Copyright (c) 2022 Macronix. | |||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please use you own copyright.
drivers/spi/ospi_ll_stm32.c
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why "ll", you're using HAL API
drivers/spi/Kconfig.stm32_ospi
Outdated
| bool "STM32 MCU OSPI controller driver" | ||
| default y | ||
| depends on SOC_FAMILY_STM32 | ||
| select USE_STM32_LL_OSPI |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You're not using ll API, not required
drivers/spi/Kconfig.stm32_ospi
Outdated
| #if SPI_STM32 | ||
|
|
||
| #config SPI_STM32_INTERRUPT | ||
| # bool "STM32 MCU SPI Interrupt Support" | ||
| # help | ||
| # Enable Interrupt support for the SPI Driver of STM32 family. | ||
|
|
||
| #config SPI_STM32_DMA | ||
| # bool "STM32 MCU SPI DMA Support" | ||
| # select DMA | ||
| # help | ||
| # Enable the SPI DMA mode for SPI instances | ||
| # that enable dma channels in their device tree node. | ||
|
|
||
| #config SPI_STM32_USE_HW_SS | ||
| # bool "STM32 Hardware Slave Select support" | ||
| # default y | ||
| # help | ||
| # Use Slave Select pin instead of software Slave Select. | ||
|
|
||
|
|
||
| #endif # SPI_STM32 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If not needed, please clean it.
|
|
||
| include: [base.yaml, power.yaml] | ||
|
|
||
| on-bus: spi |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should be properly explained as it will have many impacts.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What we want to do is to make spi_nor.c compatible with ospi and qspi controllers ("bus: qspi" and "bus: ospi") such as STM32 qspi/ospi controller, not just limited to spi controller ("bus: spi"). But "on-bus: spi" property bings the compatible "jedec_spi_nor" with "bus: spi".
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also , I found dts/bindings/mtd/nxp,xspi-device.yaml includes spi-device.yaml. In other words, NXP xspi nodes are binding to spi controller ("bus: spi") because the "on-bus: spi" property. This can work now is because the NXP flexspi controller has no "bus: ospi" or "bus: qspi" property (dts/bindings/spi/nxp,imx-flexspi.yaml).
include/zephyr/drivers/spi.h
Outdated
| struct spi_buf { | ||
| void *buf; | ||
| size_t len; | ||
| #ifdef CONFIG_SPI_EXTENDED_MODES |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should be done in a dedicated commit.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should be done in a dedicated commit.
Do we need to submit a new pull request for it? Or can there be multiple commits in a pull request?
| compatible = "st,stm32-ospi-nor"; | ||
| compatible = "jedec,spi-nor"; | ||
| label = "MX25LM51245"; | ||
| reg = <0>; | ||
| ospi-max-frequency = <DT_FREQ_M(50)>; | ||
| size = <DT_SIZE_M(512)>; /* 64 MBytes */ | ||
| spi-bus-width = <OSPI_OPI_MODE>; | ||
| data-rate = <OSPI_DTR_TRANSFER>; | ||
| four-byte-opcodes; | ||
| spi-max-frequency = <DT_FREQ_M(50)>; | ||
| size = <DT_SIZE_M(64)>; | ||
| jedec-id = [c2 85 3a]; | ||
| duplex = <0>; | ||
| frame-format = <0>; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please explain these changes
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What we wanted to do before was to make spi_nor.c compatible with ospi controller and nodes. If this is possible in the future, these nodes such as mx25lm51245g will be compatible with "jedec,spi-nor".
de-nordic
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
IMHO there is more work needed to cut down what is compiled in and allow to select which modes are supported.
As far as I can tell the bits that state SPI line configuration are set at compile time, for example with SPI_CONFIG_DT_INST, which means that most of mode switching code will never be used as the design of hardware exactly specifies by how many lines the SPI device is connected.
A designer of hardware should be allowed to pre-select supported modes and cut support to only these, to cut on flash usage by the driver.
drivers/flash/spi_nor.c
Outdated
| #ifdef CONFIG_SPI_EXTENDED_MODES | ||
|
|
||
| rc = spi_nor_change_protocol(dev, spi_config_get_lines(spi_cfg)); | ||
| //rc = spi_nor_change_protocol(dev, PROTO_8_8_8); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Leftovers.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
solved
tbursztyka
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If you really need quad/octal modes, please refer to #39991
Which is a more generic SPI API update than yours. (ALL generic SPI specific configurations bits should land in spi.h, but it should stay generic and provide no specialization towards a use case such as nor flash for instance).
I would be interested to know if you could use that API update instead.
Planning to reopen the PR? |
Sure |
|
This pull request has been marked as stale because it has been open (more than) 60 days with no activity. Remove the stale label or add a comment saying that you would like to have the label removed otherwise this pull request will automatically be closed in 14 days. Note, that you can always re-open a closed pull request at any time. |
|
@daniel-0723 You can continue your work. Based on the discussion, there are still some unresolved issues. |
|
This pull request has been marked as stale because it has been open (more than) 60 days with no activity. Remove the stale label or add a comment saying that you would like to have the label removed otherwise this pull request will automatically be closed in 14 days. Note, that you can always re-open a closed pull request at any time. |
|
Hi @de-nordic @rruuaanng , I have updated this PR's branch, please remove the "stale" label and review it. |
|
@daniel-0723 You may temporarily adopt the suggestions made by the other maintainers mentioned above, as they appear to be incomplete. Once they are completed, I will review them again. |
1464b46 to
eb75cb9
Compare
Many Flash have low power and high performance modes. Also, Flash in 8D-8D-8D mode consumes more power than in 1S-1S-1S but of high performance, which requires the Flash mode can be switched in driver code to meet low power and high performance scenario. |
95284a5 to
9e9b4a8
Compare
Enable quad and octal mode in spi_nor.c Signed-off-by: Daniel Zhang <[email protected]>
9e9b4a8 to
9cc87b2
Compare
|
I already told you this needs to be redone almost completely.
Again, I proposed years ago #39991 , nobody backed it up (sure it would have needed changes and refinements). In meetings it was decided that it was unnecessary, that qspi and all could get a direct flash driver, and no other device would ever need it. And then this happened: #71769 |
Thank you for your comment. I think maybe I should rework it and move these work to drivers/flash/flash_mspi_nor.c. |
|
This pull request has been marked as stale because it has been open (more than) 60 days with no activity. Remove the stale label or add a comment saying that you would like to have the label removed otherwise this pull request will automatically be closed in 14 days. Note, that you can always re-open a closed pull request at any time. |
| cr2 = 0x02; | ||
| ret = spi_nor_wrcr2(dev, cr2); | ||
| data->nor_protocol = PROTO_8D_8D_8D; | ||
| break; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
default case missing
| static int spi_nor_read(const struct device *dev, off_t addr, void *dest, | ||
| size_t size) | ||
| { | ||
| struct spi_nor_data *data = dev->data; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This will trigger a build warning on data being unused when CONFIG_SPI_EXTENDED_MODES is disabled.
Suggestion: at line 1052:
- struct spi_nor_data *data = dev->data;
+ uint8_t read_cmd = SPI_NOR_CMD_READ;
const size_t flash_size = dev_flash_size(dev);
int ret;
+ if (IS_ENABLED(CONFIG_SPI_EXTENDED_MODES))
+ read_cmd = ((struct spi_nor_data *)(dev->data))->read_cmd;Ditto in spi_nor_write().
| nor_protocol = PROTO_1_1_1; | ||
| } | ||
|
|
||
| return nor_protocol; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nitpicking suggestion:
static enum spi_nor_protocol spi_config_get_lines(const struct spi_config *config)
{
switch (config->operation & SPI_LINES_MASK) {
case SPI_LINES_SINGLE:
return PROTO_1_1_1;
case SPI_LINES_DUAL:
return PROTO_1_2_2;
case SPI_LINES_QUAD:
return PROTO_4_4_4;
case SPI_LINES_OCTAL:
return PROTO_8_8_8;
default:
return PROTO_1_1_1;
}
}| #endif | ||
|
|
||
| #ifdef CONFIG_SPI_EXTENDED_MODES | ||
|
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you remove this empty line?
| */ | ||
| size_t len; | ||
|
|
||
| #ifdef CONFIG_SPI_EXTENDED_MODES |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Prefer without this tabulation indentation.
| data->read_cmd = SPI_NOR_CMD_READ; | ||
| data->program_cmd = SPI_NOR_CMD_PP; | ||
| data->erase_cmd = SPI_NOR_CMD_SE; | ||
| break; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could shrink the base cases:
case PROTO_1_1_1:
case PROTO_1_1_2:
case PROTO_1_2_2:
case PROTO_1_1_4:
case PROTO_1_4_4:
case PROTO_1_4D_4D:
data->nor_protocol = nor_protocol;
data->flag_access_32bit = false;
data->read_cmd = SPI_NOR_CMD_READ;
data->program_cmd = SPI_NOR_CMD_PP;
data->erase_cmd = SPI_NOR_CMD_SE;
break;| int ret = spi_nor_cmd_write(dev, SPI_NOR_CMD_WREN); | ||
|
|
||
| if (ret == 0) { | ||
|
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you remove this empty line?
| * function. | ||
| * | ||
| * @param dev Device struct | ||
| * @param sr The new value of the configuration register2 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Replace sr with cr.
|
This pull request has been marked as stale because it has been open (more than) 60 days with no activity. Remove the stale label or add a comment saying that you would like to have the label removed otherwise this pull request will automatically be closed in 14 days. Note, that you can always re-open a closed pull request at any time. |
This PR is to support SPI extended modes, like 1-1-2/1-2-2/1-1-4/1-4-4/1-4D-4D/4-4-4/8-8-8/8D-8D-8D.
1-4D-4D :
1 means command is sent on 1 line in STR mode;
the first 4D means address is sent on 4 lines in DTR mode;
the second 4D means data is sent on 4 lines in DTD mode;
8D-8D-8D :
the first 8D means command is sent on 8 lines in DTR mode;
the second 8D means address is sent on 8 lines in DTR mode;
the third 8D means data is sent on 8 lines in DTD mode;